package garooyaproject.android.widget.better;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.TextView;

public class BetterSeekBar extends SeekBar {

	private static final int INVALID_POINTER_ID = -1;
	private static final int INIT_IMTES = 1;
	private int firstPointerId = INVALID_POINTER_ID;
	private int secondPointerId = INVALID_POINTER_ID;
	private float basisY = 0;
	private float basisX = 0;
	private double times = INIT_IMTES;
	private double oldTimes = INIT_IMTES;
	
	private WindowManager.LayoutParams layoutParams;
	private TextView tv;
	
	private Context context;
	
	public BetterSeekBar(Context context) {
		super(context);
		this.context = context;
	}

	public BetterSeekBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public BetterSeekBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		final int action = event.getAction();
		
		switch (action & MotionEvent.ACTION_MASK) {
		
		case MotionEvent.ACTION_DOWN:
			Log.d("BSEEKBAR", "ACTION_DOWN");
			if(firstPointerId == INVALID_POINTER_ID) {
				firstPointerId =  event.getPointerId(0);
			}
			break;
			
		case MotionEvent.ACTION_UP:
			Log.d("BSEEKBAR", "ACTION_UP");
			times = INIT_IMTES;
			oldTimes = INIT_IMTES;
			return true;
		
		case MotionEvent.ACTION_POINTER_DOWN:
			Log.d("BSEEKBAR", "ACTION_POINTER_DOWN");
			if(firstPointerId == INVALID_POINTER_ID) return true;			
			if(event.getPointerCount() > 1) {
				if(secondPointerId == INVALID_POINTER_ID) {
					secondPointerId =  event.getPointerId(1);
					basisY = event.getY(1);
					basisX = event.getX(0);
					initiateWindow(context);
				}
				return true;
			}

			break;

		case MotionEvent.ACTION_POINTER_UP:
			Log.d("BSEEKBAR", "ACTION_POINTER_UP");

			if(event.getPointerId(event.getActionIndex()) == firstPointerId) {
				firstPointerId = INVALID_POINTER_ID;
				secondPointerId = INVALID_POINTER_ID;
				hideText();
				return true;
			}

			if(event.getPointerId(event.getActionIndex()) == secondPointerId) {
				secondPointerId = INVALID_POINTER_ID;
				basisY = 0;
				oldTimes = times;
				hideText();
				return true;
			}
			
			break;			
			
		case MotionEvent.ACTION_MOVE:
			Log.d("BSEEKBAR", "ACTION_MOVE");
			
			if(firstPointerId == INVALID_POINTER_ID) return true;
			if(event.getPointerCount() > 1) {				
				if(secondPointerId != INVALID_POINTER_ID) {
					int pointerIndex = event.findPointerIndex(secondPointerId);
					float distance = basisY - event.getY(pointerIndex);
					int power = (((int) distance) / 100);
					times = oldTimes * Math.pow(2,power);
					showText(context, Double.toString(times));	
				}
				return true;
			} else if(event.getPointerCount() == 1 &&
					event.getPointerId(event.getActionIndex()) == firstPointerId) {

				float x = basisX + ((event.getX() - basisX) * (float) times);
				event = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), action, x, event.getY(), event.getMetaState());
			}
			break;
			
		default:
			break;
		}
		
		return super.onTouchEvent(event);
	}
	
	
	private void initiateWindow(Context context) {
			
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Point outSize = new Point();
			wm.getDefaultDisplay().getSize(outSize);
			
			int centerY = outSize.y / 2;
			int y = outSize.y / 3;
			int top = getTop();
			if(top > centerY) {
				y = - y;
			}
			
		
			layoutParams = new WindowManager.LayoutParams(
					WindowManager.LayoutParams.WRAP_CONTENT, 
					WindowManager.LayoutParams.WRAP_CONTENT,
					0, y,
					WindowManager.LayoutParams.TYPE_TOAST, 
					WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
						| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
					PixelFormat.TRANSLUCENT);
			
			
			tv = new TextView(context);
			tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tv.setTextSize(20);
			tv.setText("");
			
			tv.setPadding(0, 0, 0, 0);

			Paint paint = new Paint();
			paint.setColor(Color.BLACK);
			paint.setAlpha(150);
			
			tv.setBackgroundColor(paint.getColor());
			
			wm.addView(tv, layoutParams);
	}
	
	private void showText(Context context, String text) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		wm.updateViewLayout(tv, layoutParams);
		
		tv.setPadding(10, 5, 10, 5);
		tv.setText(text);
		tv.invalidate();
	}
	
	private void hideText() {
		tv.setText("");
		tv.setPadding(0, 0, 0, 0);
		tv.invalidate();
	}
	
}
